package com.tiger.distributelock.config;

import org.apache.zookeeper.ZooKeeper;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;

@Configuration
public class ZooKeeperConfig {

    @Value("${zookeeper.connectString}")
    private String connectString;
    @Value("${zookeeper.sessionTimeout}")
    private int sessionTimeout = 2000;

    @Bean(name = "zookeeper", destroyMethod = "close")
    public ZooKeeper zooKeeper(ZooKeeperWatcher watcher) {
        try {
            ZooKeeper zooKeeper = new ZooKeeper(connectString, sessionTimeout, watcher);
            return zooKeeper;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
